package com.text;


import com.Service.UserService;
import com.factory.FactoryMethod;
import com.javabean.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Calendar;

public class MyTest {
    @Test
    public void live1(){
        //生命周期
        ClassPathXmlApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        UserService u1 = (UserService) applicationContext.getBean("u3");
        UserService u2 = (UserService) applicationContext.getBean("u3");
        System.out.println(u1==u2);
        System.out.println(u1.hashCode()+"|"+u2.hashCode());
        applicationContext.close();
    }
    @Test
    public void scope2(){
        //多例
        ClassPathXmlApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        UserService u1 = (UserService) applicationContext.getBean("u2");
        UserService u2 = (UserService) applicationContext.getBean("u2");
        System.out.println(u1==u2);
        System.out.println(u1.hashCode()+"|"+u2.hashCode());
        applicationContext.close();
    }
    @Test
    public void scope1(){
        //单例
        ClassPathXmlApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        UserService u1 = (UserService) applicationContext.getBean("userService");
        UserService u2 = (UserService) applicationContext.getBean("userService");
        System.out.println(u1==u2);
        System.out.println(u1.hashCode()+"|"+u2.hashCode());
        applicationContext.close();
    }
    @Test
    public void test(){
        //1 加载配置文件创建spring容器对象 ApplicationContext容器对象接口
        //1.1 ClassPathXmlApplicationContext 用于读取类路径(resource)下 xml配置文件创建spring容器对象的工具类(常用)
        //1.2 FileSystemXmlApplicationContext 用于读取文件系统中的 xml配置创建spring容器对象的工具类
        //1.3 AnnotationConfigApplicationContext 使用注解配置类创建spring容器对象的工具类
        ClassPathXmlApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        //2 使用getBean方法获取spring中的对象
        //2.1 getBean(String id) 通过配置的唯一id获取对应的对象 只不过因为不知道对象的类型 所以返回Object类型 使用时需要强转
        UserService u1 = (UserService) applicationContext.getBean("userService");
        //2.2 getBean(Class c) 通过class类型进行匹配 返回适配类型(指定类型或指定类型的子类)
        //注意 如果有相同的class会报错
        //UserService u2 = applicationContext.getBean(UserService.class);
        //3.使用
        u1.save();
        //u2.save();
        //4.关闭容器 close方法是对应实现类额外的方法
        applicationContext.close();
    }
    @Test
    public void u4(){
        FactoryMethod factoryMethod=new FactoryMethod();
        User user = factoryMethod.getUser();

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Object u3 = applicationContext.getBean("u4");
        System.out.println(u3);
        applicationContext.close();
    }
    @Test
    public void c(){
        Calendar calendar;
        //calendar=new Calendar();
        calendar=Calendar.getInstance();

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Object c = applicationContext.getBean("c");
        System.out.println(c);
        applicationContext.close();
    }
    @Test
    public void u5(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Object u5 = applicationContext.getBean("u5");
        System.out.println(u5);
        applicationContext.close();
    }
    @Test
    public void u6(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Object u6 = applicationContext.getBean("u6");
        System.out.println(u6);
        applicationContext.close();
    }

}
